#include<bits/stdc++.h>
using namespace std;
int n,m,a[510],au[510],mm[510],auu[510],dd[8],dr[8][500],T;
bool f[510];
int main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=m;i++)cin>>mm[i];
	int k=0,kl=1;
	while(kl<n)kl*=2,k++;
	for(int i=1;i<=k;i++){
		string drr;cin>>drr;
		dd[i]=drr.size();
		for(int i=0;i<dd[i];i++)dr[k][i+1]=(int)(drr[i]-'0');
	}
	cin>>T;
	while(T--){
		int z[4];
		cin>>z[0]>>z[1]>>z[2]>>z[3];
		for(int i=1;i<=n;i++)au[i]=(a[i]^z[i%4]);
		for(int i=1;i<=m;i++){
			memset(auu,0,sizeof(auu));memset(f,0,sizeof(f));
			int p=2;
			while(p*2<=mm[i])p*=2;
			for(int i=1;i<=p;i++)auu[i]=au[i];
		}
		cout<<1<<'\n';
	}
	return 0;
}
